package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.adapter.common.ArrayListAdapter;
import cn.edu.zjicm.wordsnet_d.data.VoicePack;
import cn.edu.zjicm.wordsnet_d.download.DownloadVoicePackManager;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.util.VoicePackViewHolder;
import cn.edu.zjicm.wordsnet_d.view.RoundProgressBar;

public class VoicePackAdapter extends ArrayListAdapter<VoicePack> {
	private Activity mContext;
	private LayoutInflater mInflater;
	private ArrayList<VoicePack> mList;
	
	public VoicePackAdapter(Activity mContext) {
		this.mContext = mContext;
		this.mList = VoicePack.packList;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public ArrayList<VoicePack> getmList() {
		return mList;
	}

	public void setmList(ArrayList<VoicePack> mList) {
		this.mList = mList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public VoicePack getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final VoicePackViewHolder mHolder;
//		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.view_voice_pack_item, null);
			mHolder = new VoicePackViewHolder();
			mHolder.download_state = (ImageView) convertView.findViewById(R.id.article_has_download_img);
			mHolder.nameTv = (TextView) convertView.findViewById(R.id.voice_pack_name);
			mHolder.nameTv.setEllipsize(TextUtils.TruncateAt.END);
			mHolder.descriptionTv = (TextView) convertView.findViewById(R.id.voice_pack_description);
			mHolder.descriptionTv.setEllipsize(TextUtils.TruncateAt.END);
			mHolder.sizeTv = (TextView) convertView.findViewById(R.id.voice_pack_size);
			mHolder.downloadFlipper = (ViewFlipper) convertView.findViewById(R.id.download_flipper);
			mHolder.roundProgressBar = (RoundProgressBar) convertView.findViewById(R.id.roundProgressBar);
			mHolder.fakeDownloadBtn = (RelativeLayout) convertView.findViewById(R.id.fake_download_btn);
			mHolder.divider = (TextView) convertView.findViewById(R.id.add_school_divider);
			mHolder.itemLayout = (RelativeLayout) convertView.findViewById(R.id.voice_item_layout);
			
			convertView.setTag(mHolder);
		}

		final VoicePack voicePack = mList.get(position);
		
		mHolder.nameTv.setText(voicePack.getName());
		mHolder.descriptionTv.setText(voicePack.getDescription());
		mHolder.sizeTv.setText(voicePack.getShowSize() + "M");

		if (DownloadVoicePackManager.isDownloadManagerExist(voicePack.getName())) {
			DownloadVoicePackManager downloadManager = DownloadVoicePackManager.getInstance(mContext, voicePack.getName(), null, true);
			downloadManager.showDownloadingLayout(mHolder, voicePack);
			mHolder.downloadFlipper.setDisplayedChild(1);
		} else if (DownloadVoicePackManager.checkDownload(voicePack)) {
			mHolder.downloadFlipper.setDisplayedChild(0);
			mHolder.download_state.setImageResource(R.drawable.no_download);
		} else {
			mHolder.downloadFlipper.setDisplayedChild(0);
			mHolder.download_state.setImageResource(R.drawable.download_complete);
		}

		mHolder.fakeDownloadBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(mContext, mArticle.getName(), Toast.LENGTH_SHORT).show();
				if (mHolder.downloadFlipper.getDisplayedChild() == 0 && DownloadVoicePackManager.checkDownload(voicePack)) {

					JudgeNetwork judgeNetwork = JudgeNetwork.getInstance(mContext);
					if (judgeNetwork.isWifiConnected() || DownloadVoicePackManager.isDownloadManagerExist(voicePack.getName())) {
						DownloadVoicePackManager downloadManager = DownloadVoicePackManager.getInstance(mContext, voicePack.getName(), null, true);
						downloadManager.startToDownload();
						downloadManager.showDownloadingLayout(mHolder, voicePack);
						mHolder.downloadFlipper.setDisplayedChild(1);
					} else if (judgeNetwork.isMobileConnected()) {
						DownloadVoicePackManager.showMobileConnectedDialog(mContext, new OnCancelListener() {
							@Override
							public void onCancel(DialogInterface arg0) {
								// TODO Auto-generated method stub
								DownloadVoicePackManager downloadManager = DownloadVoicePackManager.getInstance(mContext, voicePack.getName(), null, true);
								downloadManager.startToDownload();
								downloadManager.showDownloadingLayout(mHolder, voicePack);
								mHolder.downloadFlipper.setDisplayedChild(1);
							}
						}, false);
					} else {
						Toast.makeText(mContext, "网络未连接，无法下载", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		
		if (NightModeUtil.isNightModeNow())
			mHolder.divider.setVisibility(View.GONE);
		else
			mHolder.divider.setVisibility(View.VISIBLE);
		
		if(position==0){
			Utils.setListItemTop(mHolder.itemLayout);
		}else if(position==mList.size()-1){
			Utils.setListItemBottom(mHolder.itemLayout,mHolder.divider);
		}

		return convertView;
	}

}
